﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using KBEngine.Setting.TypeDef;

namespace KBEngineTool.UI
{
    public partial class UserTypeWidget : UserControl
    {
        UserType m_type;
        string m_typeName;

        public UserTypeWidget(UserType userType)
        {
            InitializeComponent();
            m_type = userType;

            Initialize();
        }

        private void Initialize()
        {
            m_txt_typeName.Text = m_type.TypeName;
            m_txt_implementedBy.Text = m_type.ImplementedBy;

            int index = 0;
            foreach (var item in m_type.Properties)
            {
                NewProperty(item, index++);
            }
            KBEngine.Event.fireOut(MainWindow_Event.RefreshPanel);
        }

        private void UserTypeWidget_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ContextMenu con = new ContextMenu();

                MenuItem newType = new MenuItem("添加属性");
                newType.Click += new EventHandler(Click_NewProperty);

                con.MenuItems.Add(newType);

                ContextMenu = con;
            }
        }

        private void Click_NewProperty(object sender, EventArgs e)
        {
            var field = new UserType.Field();

            m_type.Properties.Add(field);

            NewProperty(field, m_type.Properties.Count - 1);

            KBEngine.Event.fireOut(MainWindow_Event.RefreshPanel);
        }

        private void NewProperty(UserType.Field field, int index)
        {
            var widget = new PropertyWidget(m_type, field, new Point(Location.X + Size.Width + 30, Location.Y + Size.Height), index);
            widget.Location = new Point(Location.X + Size.Width + 30, Location.Y + Size.Height + (widget.Size.Height + 10) * index);

            KBEngine.Event.fireOut(MainWindow_Event.AddWidget, widget,
            new Func<Point>(() => { return PointToScreen(Point.Add(m_label_properties.Location, m_label_properties.Size)); }),
            new Func<Point>(() => { return widget.PointToScreen(Point.Empty); }),
            false);
        }

        private void m_txt_typeName_TextChanged(object sender, EventArgs e)
        {
            m_type.TypeName = m_txt_typeName.Text;
            KBEngine.Event.fireOut(MainWindow_Event.UpdateNodeName, m_type);
            //m_txt_implementedBy.Text = m_type.TypeName + "." + m_type.TypeName.ToLower() + "_inst";
        }

        private void m_txt_implementedBy_TextChanged(object sender, EventArgs e)
        {
            m_type.ImplementedBy = m_txt_implementedBy.Text;
        }

        private void m_txt_typeName_Leave(object sender, EventArgs e)
        {
            if (EntityDef.AllTypes.Any(a => a != m_type && a.TypeName.ToLower() == m_type.TypeName.ToLower()))
            {
                m_type.TypeName = m_typeName;
                KBEngine.Event.fireOut(MainWindow_Event.UpdateNodeName, m_type);
                m_txt_typeName.Text = m_type.TypeName;
                MessageBox.Show("TypeName must be unique");
            }
        }

        private void m_txt_typeName_Enter(object sender, EventArgs e)
        {
            m_typeName = m_type.TypeName;
        }
    }
}
